﻿using BigEgg.Framework.UnitTesting;
using FMStudio.Documents.ProjectDocument.Properties;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;

namespace FMStudio.Documents.ProjectDocument.Test
{
    [TestClass]
    public class ProjectDocumentTypeTest
    {
        private string TestPath1 = Path.Combine(Environment.CurrentDirectory, "Test1.fmproj");
        private string TestPath2 = Path.Combine(Environment.CurrentDirectory, "Test2.fmproj");


        [TestInitialize]
        public void TestCleanup()
        {
            if (File.Exists(TestPath1))
                File.Delete(TestPath1);
            if (File.Exists(TestPath2))
                File.Delete(TestPath2);
        }


        [TestMethod]
        public void DocumentTypeTest()
        {
            ProjectDocumentType documentType = new ProjectDocumentType();
            Assert.AreEqual(Resources.DocumentExtension, documentType.FileExtension);
            Assert.AreEqual(Resources.DocumentDescription, documentType.Description);
        }

        [TestMethod]
        public void NewDocumentTest()
        {
            ProjectDocumentType documentType = new ProjectDocumentType();
            Assert.IsTrue(documentType.CanNew());

            ProjectDocument document = documentType.New(TestPath1) as ProjectDocument;
            documentType.Save(document);
            Assert.IsTrue(File.Exists(TestPath1));
            Assert.IsNotNull(document);

            Assert.AreEqual("Test1", document.Properties.Name);
            Assert.IsFalse(document.IsModified);
            Assert.IsNotNull(document.Items);
            Assert.AreEqual(0, document.Items.Count);
        }

        [TestMethod]
        public void SaveAndOpenDocumentTest()
        {
            ProjectDocumentType documentType = new ProjectDocumentType();
            ProjectDocument document = documentType.New(TestPath1);

            Assert.IsTrue(documentType.CanSave(document));
            documentType.Save(document, TestPath1);
            Assert.IsTrue(File.Exists(TestPath1));

            Assert.IsTrue(documentType.CanOpen());
            ProjectDocument openedDocument = documentType.Open(TestPath1);

            Assert.AreEqual(document.Properties.Name, openedDocument.Properties.Name);

            //  Modify the name
            openedDocument = null;
            document.Properties.Name = "NewName";
            Assert.AreEqual("NewName", document.Properties.Name);
            //  Cannot move
            AssertHelper.ExpectedException<NotSupportedException>(() => documentType.Save(document, TestPath2));
            documentType.Save(document, TestPath1);
            Assert.IsFalse(document.IsModified);
            openedDocument = documentType.Open(TestPath1);
            Assert.AreEqual("NewName", openedDocument.Properties.Name);

            //  Modify the structure
            openedDocument = null;
            document.Items.Add(new ProjectItem() { Name = "DataFile" });
            document.Items.Add(new ProjectItem() { Name = "TableFile" }); ;
            document.Items.Add(new ProjectItem() { Name = "ChartFile" });

            Assert.AreEqual(3, document.Items.Count);

            documentType.Save(document, TestPath1);
            openedDocument = documentType.Open(TestPath1);

            Assert.AreEqual(3, openedDocument.Items.Count);
            Assert.AreEqual(1, openedDocument.Items.Where(x => x.Name == "DataFile").Count());
            Assert.AreEqual(1, openedDocument.Items.Where(x => x.Name == "TableFile").Count());
            Assert.AreEqual(1, openedDocument.Items.Where(x => x.Name == "ChartFile").Count());
        }
    }
}
